<?php
/**
 * +--------------------------------------------------------------
 * | eLuoPHP
 * +--------------------------------------------------------------
 * | Copyright (c) 2016-2018 http://qxwoo.cn All rights reserved.
 * +--------------------------------------------------------------
 * | y139w [y139w@163.com]
 * +--------------------------------------------------------------
 * | Smarty模板引擎封装类文件
 * +--------------------------------------------------------------
 */

namespace luo;


class Smarty
{

    /**
     * 当前类的对象
     * @var null
     */
    private static $_instance = null;

    /**
     * Smarty对象
     * @var null|\Smarty
     */
    private $smarty = null;

    /**
     * 模板配置信息
     * @var array|mixed|null
     */
    private $config = [];

    /**
     * Smarty constructor.
     */
    private function __construct()
    {

        // 获取模板配置
        $this->config = Config::get('template');
        // 请求对象
        $request = Request::instance();
        // 模板路径
        $template_dir = trim($this->config['template_dir']);
        // 编译路径
        $compile_dir = $this->config['compile_dir'];
        // 缓存路径
        $cache_dir = $this->config['cache_dir'];
        // 是否缓存
        $caching = $this->config['caching'];
        // 左定界符
        $left_delimiter = $this->config['left_delimiter'];
        // 右定界符
        $right_delimiter = $this->config['right_delimiter'];
        // 处理模板路径
        if(empty($template_dir)){
            $template_dir = ROOT_PATH . APP_PATH . $request->module() . '/view/';
        }
        // 引入模板引擎
        include dirname(__FILE__) . '/smarty/Smarty.class.php';
        // 实例化模板引擎对象
        $smarty = new \Smarty();
        // 设置是否缓存
        $smarty ->caching = $caching;
        // 设置模板路径
        $smarty->setTemplateDir($template_dir);
        // 设置编译路径
        $smarty->setCompileDir($compile_dir);
        // 设置配置目录
        //$smarty->setConfigDir('configs/');
        // 设置缓存路径
        $smarty->setCacheDir($cache_dir);
        // 设置左定界符
        $smarty->left_delimiter = $left_delimiter;
        // 设置右定界符
        $smarty->right_delimiter = $right_delimiter;
        // 引擎对象
        $this->smarty = $smarty;
        //var_dump($smarty);

    }

    /**
     * 防止克隆
     */
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    /**
     * 静态方法获取当前对象
     * @return null|Smarty
     */
    public static function instance(){

        if( !(self::$_instance instanceof self) ){
            self::$_instance = new self();
        }
        return self::$_instance;

    }

    /**
     * 模板变量设置
     * @param $tpl_var
     * @param null $value
     * @param bool $nocache
     * @return bool
     */
    public function assign($tpl_var, $value = null, $nocache = false){

        if(is_array($tpl_var)){
            $this->setTplVar($tpl_var);
            return true;
        }
        $this->smarty->assign($tpl_var, $value, $nocache);

    }

    /**
     * 输出页面内容
     * @param null $template
     * @param null $data
     * @param null $cache_id
     * @param null $compile_id
     * @param null $parent
     * @throws \Exception
     * @throws \SmartyException
     */
    public function display($template = null, $data = null, $cache_id = null, $compile_id = null, $parent = null){

        if(!empty($data)){
            $this->setTplVar($data);
        }
        $template = $this->getFile($template);
        $this->smarty->display($template, $cache_id, $compile_id, $parent);

    }

    /**
     * 返回页面内容
     * @param null $template
     * @param null $data
     * @param null $cache_id
     * @param null $compile_id
     * @param null $parent
     * @return string
     * @throws \Exception
     * @throws \SmartyException
     */
    public function fetch($template = null, $data = null, $cache_id = null, $compile_id = null, $parent = null){

        if(!empty($data)){
            $this->setTplVar($data);
        }
        $template = $this->getFile($template);
        $fetch = $this->smarty->fetch($template, $cache_id, $compile_id, $parent);
        return $this->tplConst($fetch);

    }

    /**
     * 数组方式设置模板变量
     * @param array $data
     */
    private function setTplVar(Array $data){

        if(is_array($data)){
            foreach($data as $key=>$val){
                $this->assign($key, $val);
            }
        }
        return ;

    }

    /**
     * 解析模板变量
     * @param $content
     * @return mixed
     */
    private function tplConst($content){

        $root = str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname($_SERVER['SCRIPT_FILENAME']));
        foreach($this->config['template_const'] as $key=>$val){
            $content = str_replace($key, $root . $val, $content);
        }
        return $content;

    }

    /**
     * 模板文件处理
     * @param null $template
     * @return null|string
     */
    private function getFile($template = null){

        $request = Request::instance();
        $action = $request->action();
        $ext = $this->config['template_ext'];

        if(empty($template)){
            $template = $action . $ext;
        }

        if(!strpos($template, $ext)){
            $template .= $ext;
        }

        return $template;

    }

}